﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using ComLib.ConfigService;
using ComLib.ComType;
using ComLib.ComFun;
using System.IO;
using System.Reflection;
using LogLib;
using System.Windows.Forms;

namespace IPCPages
{
    [XmlRootAttribute("FRPOption", Namespace = "IPCPages", IsNullable = false)]
    public class FRPOption : XmlSerializeBase
    {
        [XmlIgnore]
        public override string ConfigDirectory
        {
            get
            {
                string directory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().CodeBase);
                directory = directory.Substring(6);
                return directory;
            }
        }

        [XmlIgnore]
        public string TemplateFilePath
        {
            get 
            {
                return Path.Combine(ConfigDirectory, "Config", _templateFile);
            }
        }

        /// <summary>
        /// Printer
        /// </summary>
        private string _printer = "DS-RX1";
        [XmlElementAttribute("printer", IsNullable = true)]
        public string Printer
        {
            get { return _printer; }
            set { _printer = value; }
        }

        /// <summary>
        /// 模板文件名称
        /// </summary>
        private string _templateFile = "自助照相照片打印模板.frx";
        [XmlElementAttribute("templateFile", IsNullable = true)]
        public string TemplateFileName
        {
            get { return _templateFile; }
            set { _templateFile = value; }
        }

        /// <summary>
        /// 选择是否打印
        /// </summary>
        private bool _enableChoosePrint = true;
        [XmlElementAttribute("enableChoose")]
        public bool EnableChoosePrint
        {
            get { return _enableChoosePrint; }
            set { _enableChoosePrint = value; }
        }

        public FRPOption()
        {
            _strConfigPath = "";
        }
      
        public override bool Serialize()
        {
            return Serializer.SaveToXml(ConfigFilePath, this);
        }

        public override bool Load()
        {
            FRPOption obj = null;
            try
            {
                obj = (FRPOption)Serializer.LoadFromXml(ConfigFilePath, typeof(FRPOption));               
            }
            catch (Exception ex)
            {
                Log.WriteException(ex.ToString());
                MessageBox.Show(ex.ToString());
                return false;
            }
            if (obj != null)
            {
                this._nVer = obj._nVer;
                this._printer = obj._printer;
                this._templateFile = obj._templateFile;
                this._enableChoosePrint = obj._enableChoosePrint;
                return true;
            }
            else
            {
                ErrorMessage = Serializer.Message;
                return false;
            }
        }

        public override string ToString()
        {
            StringBuilder builder = new StringBuilder("FTP账户选项: ");
            Action<string, object> FillString =
                (string field, object val) =>
                {
                    if (val != null)
                    {
                        builder.Append("[");
                        builder.Append(field);
                        builder.Append("]: ");
                        builder.Append(val.ToString() + "; ");
                    }                 
                };
            FillString("打印机名称", _printer);
            FillString("模板文件", _templateFile);
            FillString("选择是否打印", _enableChoosePrint ? "是":"否");
            return builder.ToString();
        }
    }
}
